//
//  FileUploadController.swift
//  App
//
//  Created by 汤泽川 on 2019/3/30.
//

import Foundation
import Vapor

class FileUploadController: RouteCollection {
    func boot(router: Router) throws {
        router.post(UploadFileRequest.self, at: "upload_file", use: uploadFile);
    }
    
    /// 上传图片
    func uploadFile(_ req : Request, content : UploadFileRequest) throws -> Future<Response> {
        let fileInfo = content
        let fileType = fileInfo.fileType.hasPrefix(".") ? String(fileInfo.fileType.dropFirst()) : fileInfo.fileType
        let fileFolderPath = try req.make(DirectoryConfig.self).workDir + "Public/" + "resource/" + fileType + "/"
        let fileName = try fileInfo.file.saveTo(path: fileFolderPath, typeName: fileType)
        return try ResponseModel<UploadFileResponse>.successResponse(UploadFileResponse(url:"http://tzcccy.cn" + "/resource/" + fileType + "/" + fileName)).encode(for: req)
    }
}

